package fragments;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicNameValuePair;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import utils.HTTPMethod;
import utils.ServerRequest;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.yandex.yandexfotki.R;
import com.yandex.yandexfotki.activities.StartActivity;

import entities.Album;
import entities.ObjectForSend;

public class CreateAlbumFragment extends Fragment {

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View v = inflater.inflate(R.layout.createalbum, container, false);
		initViews(v);
		return v;
	}
	
	private void initViews(View v) {
	    titleCont = (EditText) v.findViewById(R.id.altitle);
		Button submit = (Button) v.findViewById(R.id.ok);
		
		submit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				submit();
			}
		});
	}
	
	private void submit() {
//		((StartActivity)getActivity()).showDialog();
//		((ProgressDialogFragment)getFragmentManager().findFragmentByTag("dialog")).destroy();
		String title = titleCont.getText().toString();
		CreateAlbumTask cat = new CreateAlbumTask(title);
//		ProgressDialogFragment dialog = (ProgressDialogFragment) ((StartActivity)getActivity()).getSupportFragmentManager().findFragmentByTag("dialog");
//		dialog.destroy();
//		
		cat.execute();
	}
	
	private class CreateAlbumTask extends ServerRequest {
		public CreateAlbumTask(String albumTitle) {
			super();
			this.albumTitle = albumTitle;
		}
		
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			Log.d("yandex",albumTitle);
			StringEntity xml;
			List<ObjectForSend> content = new ArrayList<ObjectForSend>();
			
			try {
				xml = new StringEntity("<entry xmlns=\"http://www.w3.org/2005/Atom\" xmlns:f=\"yandex:fotki\">" +
						"<title>" +
						albumTitle +
						"</title></entry>");
				xml.setContentType("application/atom+xml; charset=utf-8; type=entry");
				content.add(new ObjectForSend(ObjectForSend.STRING_CONSTANT_TYPE, xml, ""));
				
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			List<NameValuePair> headers = new ArrayList<NameValuePair>();
//			headers.add(new BasicNameValuePair("Content-type", "application/atom+xml; charset=utf-8; type=entry"));
			headers.add(new BasicNameValuePair("Authorization", "OAuth "+((StartActivity)getActivity()).getYapi().getToken()));
			
			
			method = HTTPMethod.buildPost(((StartActivity)getActivity()).getYapi().getAlbumsUrlList(), 
					headers, content, false);
		}

		@Override
		protected void onPostExecute(Document result) {
			super.onPostExecute(result);
			Element album = result.getDocumentElement();
			resultAlbum = ((StartActivity)getActivity()).getYapi().parseAlbumsEntry(album);
			AddOptionsFragment aof = (AddOptionsFragment) getActivity().getSupportFragmentManager().findFragmentByTag("add_options");
			
			aof.addNewAlbum(resultAlbum);
			getFragmentManager().popBackStack();
		}
		
		private String albumTitle;
		
	}
	
	Album resultAlbum;
	EditText titleCont;
}
